#!/usr/bin/env python3
# Copyright 2018 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

import argparse
import configparser
import pathlib
import subprocess

wd = pathlib.Path(__file__).parent
root = wd.resolve().parent

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--toolchain-prefix', required=True)
    parser.add_argument('--adb', default='adb')
    parser.add_argument('--build-dir', type=pathlib.Path,
        default=(root / 'build'))
    parser.add_argument('--build-type', default='android-arm')
    parser.add_argument('--build-name')
    parser.add_argument('--meson', default='meson')
    parser.add_argument('--meson-arg', action='append', default=[])
    return parser.parse_args()

def main():
    args = parse_args()
    if args.build_name is None:
        args.build_name = args.build_type

    binaries = {
        'exe_wrapper': [str((wd / 'adb-exe-wrapper').resolve()), args.adb],
    }
    kvs = {'c': 'clang'}
    for b in ['ar', 'strip']:
        kvs[b] = b
    for k, v in kvs.items():
        binaries[k] = args.toolchain_prefix + v

    config_start = wd / f"{args.build_type}.xcompile"
    config = configparser.ConfigParser()
    if len(config.read([config_start])) != 1:
        raise Exception(f"Invalid architecture file {config_start}")
    if not config.has_section('binaries'):
        config.add_section('binaries')
    for k, v in binaries.items():
        config['binaries'][k] = repr(v)

    args.build_dir.mkdir(parents = True, exist_ok = True)
    config_out = args.build_dir / f'{args.build_name}.xcompile'
    with config_out.open('w') as f:
        config.write(f)
    subprocess.run([args.meson,
        str(args.build_dir / args.build_name),
        str(root),
        '--cross-file', str(config_out)] + args.meson_arg)

if __name__ == '__main__':
    main()
